/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sigl.updater.modelupdater;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author uxmal
 */
public class ModuleUpdater {

    HashMap modules;

    public ModuleUpdater() {
        modules = new HashMap();
    }

    public ModuleUpdater(HashMap modules) {
        this.modules = modules;
    }

    
    // ajoute a moduleName le module usedBy, un module qui se sert de lui (dépendance inverse)
    private void addUse(String moduleName, String usedBy) {
        
        if (moduleName.equals(usedBy)) return;
        HashMap metadata = null;
        
        if (modules.containsKey(moduleName)) {
            metadata = (HashMap) modules.get(moduleName);
        } else {
            metadata = new HashMap();  
            modules.put(moduleName, metadata);
        }
        
        if (metadata.containsKey("usedBy") && metadata.get("usedBy") instanceof ArrayList) {
                    ((ArrayList)metadata.get("usedBy")).add(usedBy);
         } else {
                //System.out.println(" -> Le composant "+dep+" dont "+name+" dépend n'existe pas encore (A FAIRE).");
                ArrayList usedList = new ArrayList();
                usedList.add(moduleName);
                metadata.put("usedBy", usedList);
        }
        

    } 
    
    public void setPackageName(String moduleName, String packageName) {
        
        HashMap metadata = null;
        
        if (modules.containsKey(moduleName)) {
            metadata = (HashMap) modules.get(moduleName);

        } else {
            metadata = new HashMap();  
            modules.put(moduleName, metadata);
        }
        
        metadata.put("package", packageName);

    }
    public void setDependencies(String moduleName, ArrayList dependencies) {
                
        // DEPENDANCES
        HashMap metadata = null;
        
        if (modules.containsKey(moduleName)) {
            metadata = (HashMap) modules.get(moduleName);

        } else {
            metadata = new HashMap();  
            modules.put(moduleName, metadata);
        }
        
        metadata.put("dependencies", dependencies);

        // DEPENDANCES INVERSES
        Iterator it = dependencies.iterator();
        while (it.hasNext()){
            String name = (String) it.next();
            addUse(name, moduleName);
        }

    }
    public HashMap getModules() {
        return modules;
    }

    public void setModules(HashMap modules) {
        this.modules = modules;
    }
    public int size() {
        return modules.size();
    }
}
